package com.hiyun.web.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.hiyun.web.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 学生servlet
 */
@WebServlet(urlPatterns = "/student")
public class StudentServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = new Student("小明", 18, "男", "大数据");

        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        // 将学生对象转为json形式返回页面

        String studentJson = JSONObject.toJSONString(student);
        out.println(studentJson);
        out.close();

    }
}
